package org.example.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.order.entity.OrderDO;
import org.example.order.entity.vo.OrderPageReqVO;
import org.example.order.mapper.OrderMapper;
import org.example.order.service.IOrderService;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderDO> implements IOrderService {
    @Override
    public Long addAndGetId(OrderDO orderDO) {
        int insert = this.baseMapper.insert(orderDO);
        return insert > 0 ? orderDO.getId() : null;
    }

    @Override
    public Page<OrderDO> page(OrderPageReqVO orderPageVO) {
        Page<OrderDO> orderDOPage = new Page<>();
        orderDOPage.setCurrent(orderPageVO.getPageNum());
        orderDOPage.setSize(orderPageVO.getPageSize());
        LambdaQueryWrapper<OrderDO> wrapper = new LambdaQueryWrapper<OrderDO>()
                .eq(ObjectUtils.isNotEmpty(orderPageVO.getOrderNum()), OrderDO::getId, orderPageVO.getOrderNum())
                .like(ObjectUtils.isNotEmpty(orderPageVO.getUserName()), OrderDO::getUserName, orderPageVO.getUserName())
                .eq(ObjectUtils.isNotEmpty(orderPageVO.getPhoneNumber()), OrderDO::getPhoneNumber, orderPageVO.getPhoneNumber())
                .ne(OrderDO::getPayType, 0)
                .orderByDesc(OrderDO::getCreateTime);

        return this.baseMapper.selectPage(orderDOPage, wrapper);
    }
}
